<template>
	<view>
		<!-- <u-navbar :is-back="true" :is-fixed="true" title="当季产品查询"></u-navbar> -->
		<!-- Tab navigation -->
		<view class="fixed-top bg-white">
			<view class="px-2 mt-2 pb-2">
				<u-search placeholder="请输入关键字 " height="60" border-color="#8ED0F2" search-icon-color="#8ED0F2"
					bg-color="#fff" v-model="keyword" :action-style="actionStyle" @input="search"></u-search>
			</view>
			<u-gap height="20" bg-color="#f7f7f7"></u-gap>
		</view>
		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 0px)" 
		:show-scrollbar="false"@scrolltolower="loadMore" @scrolltoupper="refresh">
			<view class="p-2" style="margin-top: 60px">
				<view class="dfex u-lines pb-2" v-for="(item, index) in list">
					<view class="lineHight">
						<view class="text-333333 font-14">{{item.productName || '-'}}</view>
						<view class="text-A0A0A0 font-12">产品品类：{{ item.productTypeName || '-' }}</view>
						<view class="text-A0A0A0 font-12">基地名称：{{ item.baseName || '-' }}</view>
					</view>
					<view class="bg-78AB06 py-1 pl-2 pr-2 rounded-circle font-14 text-white"
						@click="open('tabbar/index/AgricultureDetails', item)">查看详情</view>
				</view>
			</view>
			<view v-if="!hasMore && list.length > 0" class="text-center mt-3 mb-3 pb-2">
				<view class="text-muted">没有更多数据了</view>
			</view>
			<view v-else-if="!hasMore && list.length === 0" class="text-center mt-3 mb-3">
				<view class="icon-sousuo1 iconfont text-999999" style="font-size: 55px;"></view>
				<view class="text-999999 font-15">暂时木有内容呀~~</view>
			</view>
			<view v-else class="text-center mt-3 mb-3">
				<view class="btn btn-primary">加载更多</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import $H from '@/common/free-lib/request.js'
	export default {
		data() {
			return {
				scrollinto: '',
				keyword: '',
				actionStyle: {
					color: '#8ED0F2',
				},
				list: [],
				form: {
					pageNo: 1,
					pageSize: 10,
					total: 0 // 总页数
				},
				hasMore: true, // 是否还有更多数据
			}
		},
		created() {
			this.getList()
		},
		methods: {
			setDefaultPageConfig() {
				this.form.pageNo = 1
				this.form.pageSize = 10
				this.form.total = 0
			},
			open(path, item) {
				uni.navigateTo({
					url: `/pages/${path}?item=${JSON.stringify(item)}`,
				})
			},
			//查询
			search() {
				if (this.keyword.trim() === '') {
					// 如果关键字为空，则显示原始数据
					this.isSearching = false;
					this.hasMore = true;
					this.list = []
					this.setDefaultPageConfig()
					this.getList()
					return;
				}
				this.setDefaultPageConfig()
				this.list = []
				this.getList()
			},
			getList() {
				$H.get(
					`/agriculture/seasonalProducts/down/list`,
					 {...this.form,
					 productName: this.keyword.trim()}, {
						token: true
					}
				).then((res) => {
					if (res.result) {
					    if (this.form.pageNo === 1) {
					        this.list = res.result.records || [];
					    } else {
					        this.list = this.list.concat(res.result.records || []);
					    }
					    this.form.total = res.result.total;
					    this.hasMore = this.list.length < this.form.total;
					} else {
					    // 如果 res.result 为 null，根据实际需求设置界面状态
					    if (this.form.pageNo === 1) {
					        this.list = [];
					    }
					    this.hasMore = false;
					}
				})
			},
			// 加载更多
			loadMore() {
			
				if (this.form.pageNo * this.form.pageSize >= this.form.total) {
					this.hasMore = false;
					return;
				}
				this.form.pageNo++;
				// 请求数据
				this.getList();
			},
			// 下拉刷新
			refresh() {
				// 重置页码为 1
				this.form.pageNo = 1;
				// 清空数据列表
				this.list = [];
				// 设置标记为有更多数据
				this.hasMore = true;
				// 重新请求数据
				this.getList();
				// 结束下拉刷新动画
				uni.$emit('stopPullDownRefresh');
			},
		},
	}
</script>

<style lang="scss" scoped>
	.tab-scroll {
		width: 100vw;
	}

	.tab-item {
		width: 50%;
		display: inline-block;
		text-align: center;
		padding: 0 10px;
		line-height: 40px;
	}
</style>